﻿using DevExpress.Data;
using DevExpress.XtraGrid;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.UC.项目成果管理.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form面料审核 : DevExpress.XtraEditors.XtraForm
    {
        private MaterialRequirementQueryFacadeDTO materialRequirementQueryFacadeDTO;

        public Form面料审核(List<WIPBatchWIthMBomDTO> wIPBatchWIthMBomDTOs, ShopRightQueryFacadeDTO shopRightQueryFacadeDTO)
        {
            InitializeComponent();
            //面料审核提交，dto根据Form预览界面传参，再调接口获取
            this.materialRequirementQueryFacadeDTO = 面料审核Service.getMaterialRequirement(wIPBatchWIthMBomDTOs);
            this.gridControl面料审核预览.DataSource = materialRequirementQueryFacadeDTO.materialRequirementDTOS;
            this.materialRequirementQueryFacadeDTO.build(shopRightQueryFacadeDTO);
        }

        private void gridView面料审核预览_DoubleClick(object sender, EventArgs e)
        {
            //双击获取上方面料号、下方一览刷新，显示同面料号订单
            String fname = this.gridView面料审核预览.GetRowCellValue(this.gridView面料审核预览.FocusedRowHandle, "fname").ToString();
            if (this.materialRequirementQueryFacadeDTO.relevantWipBatchWIthMBomDTOList.ContainsKey(fname))
                this.gridControl面料待下料.DataSource = this.materialRequirementQueryFacadeDTO.relevantWipBatchWIthMBomDTOList[fname];
            else
                this.gridControl面料待下料.DataSource = new List<WIPBatchWIthMBomDTO>();
            this.刷新();
        }
        private void 刷新()
        {
            this.gridView面料待下料.RefreshData();
            this.gridView面料待下料.ExpandAllGroups();
            this.gridView面料待下料.GroupSummary.Clear();
            //表头添加数量总计、固定单耗总计
            this.gridView面料待下料.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "batchQuantity", null, "(数量总计= {0})"), });
            this.gridView面料待下料.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "gridColumn7", null, "(固定损耗总计= {0})"), });
        }

        private void gridView面料待下料_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
            else if (e.Column.Name == "gridColumn固定损耗")
            {
                if ("0".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
            }
            else if (e.Column.Name == "gridColumn待出库标识")
            {
                if ((Boolean)e.CellValue == true)
                {
                    e.Appearance.BackColor = Color.Green;
                }
                else
                {
                    e.Appearance.BackColor = Color.White;
                }
            }
        }

        private void textBox扫描员工牌_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                if (!String.IsNullOrEmpty(this.textBox扫描员工牌.Text.Trim()))
                {
                    if (String.IsNullOrWhiteSpace(项目成果Service.get判断扫描员工码是否正确(this.textBox扫描员工牌.Text.Trim())))
                    {
                        this.textBox扫描员工牌.Clear();
                        MessageBox.Show("扫描员工码错误，请重新扫描");
                        return;
                    }
                    //生成物料需求计划
                    面料审核Service.generateMaterialRequirementPlan(this.textBox扫描员工牌.Text.Trim(), this.materialRequirementQueryFacadeDTO);
                    MessageBox.Show("面料审核完成");
                }
            }
        }
    }
}
